package com.uls.web.controller.czj;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.czj.domain.CzjBuilding;
import com.uls.czj.service.ICzjBuildingService;
import com.uls.hdh.domain.HdhUsers;
import com.uls.hdh.service.IHdhUsersService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.czj.domain.CzjLand;
import com.uls.czj.service.ICzjLandService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 土地管理Controller
 *
 * @author czj
 * @date 2024-11-10
 */
@RestController
@RequestMapping("/czj/land")
public class CzjLandController extends BaseController {
    @Autowired
    private ICzjLandService czjLandService;

    @Autowired
    private ICzjBuildingService czjBuildingService;

    @Autowired
    private IHdhUsersService hdhUsersService;
    /**
     * 查询土地管理列表
     */
    @PreAuthorize("@ss.hasPermi('czj:land:list')")
    @GetMapping("/list")
    public TableDataInfo list(CzjLand czjLand) {
        startPage();
        List<CzjLand> list = czjLandService.selectCzjLandList(czjLand);

        List<CzjLand> list1 = new ArrayList<>();
        for (CzjLand czjLand1:list){
            HdhUsers hdhUsers = hdhUsersService.selectHdhUsersByUsersId(czjLand1.getLanduser());
            czjLand1.setLandusername(hdhUsers.getUsername());
            list1.add(czjLand1);
        }
        return getDataTable(list1);
    }

    /**
     * 导出土地管理列表
     */
    @PreAuthorize("@ss.hasPermi('czj:land:export')")
    @Log(title = "土地管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CzjLand czjLand) {
        List<CzjLand> list = czjLandService.selectCzjLandList(czjLand);
        ExcelUtil<CzjLand> util = new ExcelUtil<CzjLand>(CzjLand.class);
        util.exportExcel(response, list, "土地管理数据");
    }

    /**
     * 获取土地管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('czj:land:query')")
    @GetMapping(value = "/{landid}")
    public AjaxResult getInfo(@PathVariable("landid") Long landid) {
        return success(czjLandService.selectCzjLandByLandid(landid));
    }

    /**
     * 新增土地管理
     */
    @PreAuthorize("@ss.hasPermi('czj:land:add')")
    @Log(title = "土地管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CzjLand czjLand) {
        String build = czjLand.getBuildingid();
        String[] parts = build.split(",");
        Long[] buildId = new Long[parts.length];
        for (int i = 0; i < parts.length; i++) {
            buildId[i] = Long.parseLong(parts[i]);
        }
        czjLandService.insertCzjLand(czjLand);

        List<CzjLand> czjLandList = czjLandService.selectCzjLandList(czjLand);
        czjLand.setLandid(czjLandList.get(0).getLandid());
        Long landid = czjLandList.get(0).getLandid();

        for (Long i : buildId) {
            CzjBuilding czjBuilding = new CzjBuilding();
            czjBuilding.setBuildingid(landid * 100 + i);
            czjBuilding.setBuildingname(swichbuild(i));
            czjBuilding.setLocation(czjLand.getLocation());
            czjBuilding.setTotalarea(czjLand.getArea().divide(BigDecimal.valueOf(build.length()),0,RoundingMode.FLOOR));
            czjBuildingService.insertCzjBuilding(czjBuilding);
        }
        return toAjax(czjLandService.updateCzjLand(czjLand));
    }

    public String swichbuild(Long id) {
        if (id == 1l) {
            return "楼宇A";
        } else if (id == 2l) {
            return "楼宇B";
        } else if (id == 3l) {
            return "楼宇C";
        } else if (id == 4l) {
            return "楼宇D";
        } else if (id == 5l) {
            return "楼宇E";
        } else if (id == 6l) {
            return "楼宇F";
        } else if (id == 7l) {
            return "楼宇G";
        } else {
            return "楼宇H";
        }
    }


    /**
     * 修改土地管理
     */
    @PreAuthorize("@ss.hasPermi('czj:land:edit')")
    @Log(title = "土地管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CzjLand czjLand) {
        CzjLand czjLand1 = czjLandService.selectCzjLandByLandid(czjLand.getLandid());
        CzjBuilding czjBuilding = new CzjBuilding();
        czjBuilding.setLocation(czjLand1.getLocation());
        List<CzjBuilding> czjBuildingList = czjBuildingService.selectCzjBuildingList(czjBuilding);
        if (czjBuildingList.size()!=0){
            for (CzjBuilding czjBuilding1:czjBuildingList){
                czjBuilding1.setLocation(czjLand.getLocation());
                czjBuildingService.updateCzjBuilding(czjBuilding1);
            }
        }

        return toAjax(czjLandService.updateCzjLand(czjLand));
    }

    /**
     * 删除土地管理
     */
    @PreAuthorize("@ss.hasPermi('czj:land:remove')")
    @Log(title = "土地管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{landids}")
    public AjaxResult remove(@PathVariable Long[] landids) {
        return toAjax(czjLandService.deleteCzjLandByLandids(landids));
    }
}
